-- UICampCardBonus
-- Create by huanghr
-- 奖励领取界面

-- UICampCardBonus继承自Layer
UICampCardBonus = class("UICampCardBonus", function()
    return cc.Layer:create();
end);

function UICampCardBonus.create(cardType)
    return UICampCardBonus.new(cardType);
end

-- 构造函数
function UICampCardBonus:ctor(cardType)
    self:setName("UICampCardBonus");
    self.cardType = cardType;

    -- 初始化
    local node = cc.CSLoader:createNode("layout/workshop/WorkShopSign.csb");
    self:addChild(node);
    self.node = node;

    self:registerEventCallback();

    -- 注册点击事件
    self:registerTouchEvent();

    -- 重绘
    self:redraw();

    -- 适配
    self:resize();
end

-- 注册事件处理回调函数
function UICampCardBonus:registerEventCallback()
    -- 界面析构后，需要清理下
    self:registerScriptHandler(function(ev)
        if ev == "exit" then
            cclog("UIHeroAttrib 界面析构清理");

            EventMgr.removeAll("UICampCardBonus");
        elseif ev == "enter" then
        end
    end);

    -- 注册获得焦点的回调
    EventMgr.register("UICampCardBonus", event.FOCUS_CHANGED, function(para)
        if para["get"] == "UICampCardBonus" then
            UIMgr:getCurrentScene().topMenu:show(TOPMENU_FRAME_MONEY, TOPMENU_FRAME_WORKER);
        end
    end);
end

-- 注册点击事件
function UICampCardBonus:registerTouchEvent()
    -- 注册返回按钮点击事件
    local btnBack = findChildByName(self.node, "BT/btn_back");
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- 关闭当前界面
            AudioM.playFx("button_return");
            UIMgr.getCurrentScene():removeFormByName("UICampCardBonus");
        end
    end
    AddBackEventHandler(self, btnBack, onBackClick);
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_back"));

    -- 注册升级按钮
    local btnUpgrade = findChildByName(self.node, "BT/btn_upgrade");
    local function onUpgradeClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- 显示当前佣兵营地月卡说明
            local title = getLocStr("pet_camp_card_title");
            local desc = getLocStr("pet_camp_card_desc");

            showActivityTip(title, desc);
        end
    end
    btnUpgrade:addTouchEventListener(onUpgradeClick);
    TextStyleM.setTextStyle(btnUpgrade, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnUpgrade:setTitleText(getLocStr("btn_text_help"));

    -- 注册奖励领取按钮
    local btnUpgrade = findChildByName(self.node, "CT/btn_gain");
    local function onTakeClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            if not MonthCardM.cantakeBonus("pet_camp") then
                return;
            end

            require "game/ui/form/shop/UITankUse"

            UIMgr.getCurrentScene():removeFormByName("UITankUse");
            local uiForm = UITankUse.create("pet_camp_card");
            UIMgr.getCurrentScene():addForm(uiForm);

            MonthCardM.takeDailyBonus("pet_camp");

            -- 直接关闭掉本身页面
            UIMgr.getCurrentScene():removeFormByName("UICampCardBonus");
        end
    end
    btnUpgrade:addTouchEventListener(onTakeClick);
    TextStyleM.setTextStyle(btnUpgrade, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnUpgrade:setTitleText(getLocStr("take_bonus"));
end

-- 界面重绘
function UICampCardBonus:redraw()
    self.node:stopAllActions();

    -- 取得当前月卡的等级和信息
    local cardLevel = MonthCardM.getCardLevel(self.cardType);
    local cardInfo = MonthCardM.queryCardInfo(self.cardType, cardLevel);

    -- 设置标题样式
    local titleLabel = findChildByName(self.node, "CT/template/title");
    TextStyleM.setTitleStyle(titleLabel);
    titleLabel:setString(cardInfo.name);

    -- 增益效果描述
    local descLabel = findChildByName(self.node, "CT/desc");
    local desc = getLocStr("pet_camp_card_buff");

    TextStyleM.setTextStyle(descLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN);
    descLabel = applyString(descLabel, desc);

    -- 重绘招牌图标
    -- 图标
    local iconBg = findChildByName(self.node, "CT/icon_bg");
    local fileName = "bg3";
    iconBg:loadTexture(getWorkshopStrengthenIconPath(fileName));

    local icon = findChildByName(self.node, "CT/icon");
    icon:loadTexture(getMercenaryCampIconPath("haven_cabrite"));

    self:checkTakeButton();
    self:checkShowUpgradeTip();
    self:checkUpgradeButton();

    -- 获得下次领取时间
    local timeLabel = findChildByName(self.node, "CT/time_label");
    local cardData = MonthCardM.getCardData(self.cardType);
    local startTime = TimeM.getServerTime();
    local leftDay = math.ceil((cardData.overdue - startTime) / (3600*24));

    local function tick()
        local leftTime = MonthCardM.nextBonusTime(self.cardType);
        local msg = nil;
        local descLabel = findChildByName(self.node, "CT/desc");
        if leftTime <= 0 then
            local desc = getLocStr("pet_camp_card_buff");
            TextStyleM.setTextStyle(descLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN);
            descLabel = applyString(descLabel, desc);

            startTime = TimeM.getServerTime();

            leftDay = math.ceil((cardData.overdue - startTime) / (3600*24));
            msg = string.format(getLocStr("off_next_time"), 0, 0, 0, leftDay);
            TextStyleM.setTextStyle(timeLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN);
            timeLabel = applyString(timeLabel, msg);

            -- 重绘领取按钮
            self:checkTakeButton();
        else
            local secs = math.floor(leftTime % 60);
            local hours = math.floor(leftTime / 3600);

            local mins  = math.floor((leftTime % 3600) / 60);
            msg = string.format(getLocStr("off_next_time"), hours, mins, secs, leftDay);
            TextStyleM.setTextStyle(timeLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN);
            timeLabel = applyString(timeLabel, msg);

            TextStyleM.setTextStyle(descLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN);
            descLabel = applyString(descLabel, desc);
        end
        performWithDelay(self.node, tick, 1);
    end

    tick();
end

-- 检查领取按钮
function UICampCardBonus:checkTakeButton()
    local btnTake = findChildByName(self.node, "CT/btn_gain");
    if MonthCardM.cantakeBonus(self.cardType) then
        btnTake:setBright(true);
        btnTake:setTouchEnabled(true);
    else
        btnTake:setBright(false);
        btnTake:setTouchEnabled(false);
    end
end

-- 检查升级按钮是否显示提示
function UICampCardBonus:checkShowUpgradeTip()
    local hintNode = findChildByName(self.node, "BT/btn_upgrade/icon");
    checkBlueBubbleStatus(hintNode, false, 0.75);
end

-- 说明按钮
function UICampCardBonus:checkUpgradeButton()
    local upgradeButton = findChildByName(self.node, "BT/btn_upgrade");
    upgradeButton:setVisible(true);
end

-- 播放光效
function UICampCardBonus:playUpgradeEffect()
    local icon = findChildByName(self.node, "CT/icon");
    playEffect(icon, 1180, 52, 58);
end

-- 适配
function UICampCardBonus:resize()
    local node = self.node;

    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    -- BT置于底部并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");
end